﻿using System;
using System.Data;
using System.Text;
using Besture.Bll;
using Besture.Common;


namespace Besture.UI.Admin.Common
{
    public partial class ProductClassTree : System.Web.UI.Page
    {
        public string _json = string.Empty;
        //当前类别编号
        public string _ClassID = StringHelper.Query("ClassID");
        /// <summary>
        /// 是否需要顶级类别 0：不显示 1：显示
        /// </summary>
        public string _TopLevelFlag = StringHelper.Query("TopLevel");

        protected void Page_Load(object sender, EventArgs e)
        {
            //顶级类别
            DataTable table = new QueryDac().GetDataTable("SELECT * FROM ProductClassTree(0)");
            //当前选择类别编号
            _json = ZtreeJson(table);

        }

        public string ZtreeJson(DataTable table)
        {
            //{id:1, pId:0, name: "父节点1"},
            StringBuilder stringBuilder = new StringBuilder();
            if (!string.IsNullOrEmpty(_TopLevelFlag) && _TopLevelFlag == "1")
            {
                stringBuilder.Append("{id:0, pId:0, name: '顶级类别', open:true},");
            }
            foreach (DataRow row in table.Rows)
            {
                stringBuilder.Append("{id:" + row["ID"] + ", pId:"+row["ProductClassParentID"].ToString()+", name: '" + row["Name"] + "', open:true},");
            }
            string json = string.Format(@"[{0}];", stringBuilder.ToString().TrimEnd(','));
            return json;
        }
    }
}